//
//  shuiguolaileDateTimePickerView.m
//  shuiguolaileDateTimePickerView
//
//  Created by 1 on 18/10/26.
//  Copyright © 2018年 HZQ. All rights reserved.
//

#import "shuiguolaileDateTimePickerView.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface shuiguolaileDateTimePickerView ()

@property (nonatomic, strong) NSDate *selectDate;

@property (weak, nonatomic) IBOutlet UIButton *cannelBtn;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UIView *backgVIew;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightPadding;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftPadding;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomPadding;

@end

@implementation shuiguolaileDateTimePickerView

- (void)setCustomBottomPadding:(CGFloat)customBottomPadding{
    _customBottomPadding = customBottomPadding;
    self.bottomPadding.constant = customBottomPadding;
}

+ (shuiguolaileDateTimePickerView *)instanceDatePickerView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"shuiguolaileDateTimePickerView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.backgVIew.layer.cornerRadius = 5;
    self.backgVIew.layer.borderWidth = 1;
    self.backgVIew.layer.borderColor = [[UIColor clearColor] CGColor];
    self.backgVIew.layer.masksToBounds = YES;
    
    /** sure */
    self.sureBtn.layer.cornerRadius = 4;
    self.sureBtn.layer.borderWidth = 1;
    [self.sureBtn setTitleColor:AppMianColor forState:0];
    self.sureBtn.layer.borderColor = [AppMianColor CGColor];
    self.sureBtn.layer.masksToBounds = YES;
    
    /** cancel */
    self.cannelBtn.layer.cornerRadius = 4;
    self.cannelBtn.layer.borderWidth = 1;
    self.cannelBtn.layer.borderColor = [[UIColor colorWithHexString:@"BAB9B9"] CGColor];
    self.cannelBtn.layer.masksToBounds = YES;
    
    [self showContentView:YES];
}

-(void)showContentView:(BOOL)show{
    if (show) {
        [UIView animateWithDuration:0.5 animations:^{
            self.bottomPadding.constant = 0;
        }];
    }else{
        [UIView animateWithDuration:0.3 animations:^{
          
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
}

- (NSString *)timeFormat
{
    NSDate *selected = [self.datePickerView date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentOlderOneDateStr = [dateFormatter stringFromDate:selected];
    return currentOlderOneDateStr;
}

- (void)animationbegin:(UIView *)view {
  
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 0.1;
    animation.repeatCount = -1;
    animation.autoreverses = YES;
    animation.fromValue = [NSNumber numberWithFloat:1.0];
    animation.toValue = [NSNumber numberWithFloat:0.9];
    [view.layer addAnimation:animation forKey:@"scale-layer"];
    
}

- (IBAction)blackBtnClick:(id)sender {
    [self.superview endEditing:YES];
}

- (IBAction)removeBtnClick:(id)sender {
    [self animationbegin:sender];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (IBAction)sureBtnClick:(id)sender {
    [self animationbegin:sender];
    self.selectDate = [self.datePickerView date];
    [self.delegate getSelectDate:self.selectDate type:self.type];
    [self removeBtnClick:nil];
}

@end


